package com.mb.app.service.impl;

import com.mb.app.domain.TbChanelRepotInfo;
import com.mb.app.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

@Service("reprotInfoService")
@Slf4j
public class ReprotInfoServiceImpl implements IReportInfoService {

    @Autowired
    private IChannelPvUvService channelPvUvService;

    @Autowired
    private ITbChanelRepotInfoService chanelRepotInfoService;

    @Override
    //@PostConstruct
    public void channelPvUvService() {
        while (true) {
            log.info("channelPvUvService running.....");
            try {
                this.getDataList();
            } catch (Exception e) {
                log.error("channelPvUvService Exception ",e);
            }

            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                log.error("InterruptedException",e);
            }
            log.info("channelPvUvService run finish.....");
        }
    }

    @Override
    public void insertChanelRepotInfo(Long appId, Long channelId, Long appChannelId, String reportDate) {
        TbChanelRepotInfo chanelRepotInfo = new TbChanelRepotInfo();
        chanelRepotInfo.setAppId(appId);
        chanelRepotInfo.setChannelId(channelId);
        chanelRepotInfo.setAppChannelId(appChannelId);
        chanelRepotInfo.setReportDate(reportDate);
        chanelRepotInfoService.inserts(chanelRepotInfo);
    }

    private void getDataList(){
        List<TbChanelRepotInfo> chanelRepotInfos = chanelRepotInfoService.takeOneHundred();
        try {
            if(chanelRepotInfos == null || chanelRepotInfos.size() == 0){
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    log.error("getDataList Exception ",e);
                }
            }else{
                this.dealData(chanelRepotInfos);
            }
        } catch (Exception e) {
            log.error("deal  getDataList Exception ",e);
        }
    }


    private void dealData(List<TbChanelRepotInfo> chanelRepotInfos){
        for (int i = 0; i < chanelRepotInfos.size(); i++) {
            TbChanelRepotInfo chanelRepotInfo = chanelRepotInfos.get(i);
            if (chanelRepotInfo != null) {
                Long appId = null;
                Long channelId = null;
                String reportDate = "";
                if (chanelRepotInfo.getAppId() != null) {
                    appId = chanelRepotInfo.getAppId();
                }
                if (chanelRepotInfo.getChannelId() != null) {
                    channelId = chanelRepotInfo.getChannelId();
                }
                if (chanelRepotInfo.getReportDate() != null) {
                    reportDate = chanelRepotInfo.getReportDate();
                }
                channelPvUvService.runSingle(reportDate,appId,channelId);
                chanelRepotInfoService.delete(chanelRepotInfo.getId());
            }
        }
    }

}
